package com.zn.util;

import com.zn.vo.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Properties;

/**
 * Created by yinjiawei on 2018/7/11.
 */
public class TestUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(TestUtil.class);
    public static String timeStamp2Date(String seconds,String format) {
        if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
            return "";
                    }
                if(format == null || format.isEmpty()){
            format = "yyyy-MM-dd HH:mm:ss";
        }
                 SimpleDateFormat sdf = new SimpleDateFormat(format);
                 return sdf.format(new Date(Long.valueOf(seconds+"000")));
    }

    /**
     * 校验当前时间是否在交易时间段内
     * @param tradingTime
     * @return
     */
    /**
     * 校验当前时间是否在交易时间段内
     * @param tradingTime
     * @return
     */
    /*private static boolean checkTradingTime(String tradingTime){
        boolean flag=false;
        try {
            Date nowDate=DateUtils.convertStringToDate("yyyy-MM-dd HH:mm", "2018-09-28 21:59:59");
            Integer currentTimeHour=Integer.valueOf(DateUtils.getDateTime("HH", nowDate));
            //如果第二天为节假日 则晚盘不开
            *//**dayType 工作日对应结果为 0, 休息日对应结果为 1, 节假日对应的结果为 2**//*
//            if(currentTimeHour>=21){
//                //大于21点 判断明天是否是节假日
//                Response tomorrowDay=tradeRemoteCall.checkHoliday(DateUtils.convertDateToString(DateUtils.addDay(nowDate, 1)));
//                if(tomorrowDay.isSuccess() && tomorrowDay.getDayType()==2){
//                    return flag;
//                }
//            }
//            Response response=tradeRemoteCall.checkHoliday(DateUtils.convertDateToString(nowDate));
            Response response=new Response();
            response.setResultFlag("0");
            response.setDayType(1);
            response.setWeek(6);
            *//**如果是周六 则00：00~02：30是可以交易的**//*
            if( response.isSuccess() && response.getDayType()==1){
                //如果是周六 除00:00~02:30 其他时间都是非交易时间 02:30~03:00交给时间段控制
                if(currentTimeHour>=3 && response.getWeek()==6){
                    return flag;
                }
                //如果是周日 则直接提示 非交易时间
                if(response.getWeek()==0){
                    return flag;
                }
            }
            //如果是工作日 则直接校验时间段
            if(response.isSuccess() && (response.getDayType()==0 || response.getWeek()==6)){
                String [] tradingTimes=tradingTime.split("\\|");
                for(String time:tradingTimes){
                    String [] timeSlot = time.split("-");
                    Date beginTime=null,endTime=null;
                    String nowDateStr=DateUtils.convertDateToString(DateUtils.convertStringToDate("2018-09-08"));
                    beginTime= DateUtils.convertStringToDate("yyyy-MM-dd HH:mm", nowDateStr + " " + timeSlot[0]);
                    Integer beginHour=Integer.valueOf(DateUtils.getDateTime("HH", beginTime));
                    endTime=DateUtils.convertStringToDate("yyyy-MM-dd HH:mm",nowDateStr+" "+timeSlot[1]);
                    Integer endTimeHour=Integer.valueOf(DateUtils.getDateTime("HH", endTime));
                    if(beginHour==21 && endTimeHour<23 ){
                        if(currentTimeHour>3){
                            endTime=DateUtils.addDay(endTime,1);
                        }else{
                            beginTime=DateUtils.addDay(beginTime,-1);
                        }
                    }
                    flag=DateUtils.belongCalendar(nowDate,DateUtils.addMinute(beginTime,-1),endTime);
                    if(flag){
                        break;
                    }
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }finally {
            return flag;
        }
    }*/

    public static void aaa(){
        String direction="";
        Integer currentHour=Integer.valueOf(DateUtils.getDateTime("HH", new Date()));
        Date nowDate=new Date();
        if(currentHour>=21){
            nowDate=DateUtils.addDay(nowDate,1);
        }
        String nowDateStr=DateUtils.formatDate("yyyyMMdd",nowDate);
        //如果相等 则是平今
        if(nowDateStr.equals("20180724")){
            direction="1";
        }else{
            direction="4";
        }
        LOGGER.info("String direction:" + direction);
    }
//    public static void main(String[] args) throws Exception{
////        Date date=new Date();
////        SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
////        String a=dateFm.format(date);
////        LOGGER.info("a:"+a);
//
////        String cc=timeStamp2Date("1363746981289", "yyyy-MM-dd HH:mm:ss");
////        LOGGER.info("a:"+a+"aa"+aa+";datetime:"+datetime+"cc:"+cc);
//        SimpleDateFormat sdf=new SimpleDateFormat("HH");
//        Integer newH=Integer.valueOf(sdf.format(new Date()));
//        Properties prop = System.getProperties();
////        String os = prop.getProperty("os.name");
////        LOGGER.info("newH:"+os);
////        aaa();
////        bbb();
////        int i=6;
////        LOGGER.info("i:" + Math.abs(i));
////        Long minNum=getMinNum("9:00-10:15|10:30-11:00|13:30-14:35|21:00-01:00");
////        LOGGER.info("minNum:" + minNum);
////        isNowTime();
//
////        boolean flag=checkTradingTime("9:00-10:15|10:30-11:00|13:02-13:05|21:00-02:30");
////        LOGGER.info("checkTradingTime:" + flag);
//
//
//
//    }
    public static void bbb(){
        /**做多平仓盈亏=（平仓价-持仓价）*合约乘数*成交手数，持仓价为开仓进场价**/
        BigDecimal pingcang=new BigDecimal(1476.000);
        BigDecimal kaicang=new BigDecimal(1477.000);
        BigDecimal shoushu=new BigDecimal(1);
        BigDecimal heyuechengs=new BigDecimal(20);
        BigDecimal yinkui=pingcang.subtract(kaicang).multiply(heyuechengs).multiply(shoushu);
        LOGGER.info("yinkui:" + yinkui);
    }



    public static Long getMinNum(String tradingTime){
        Long minNum=0L;
        String [] tradingTimes=tradingTime.split("\\|");
        for(String time:tradingTimes){
            String [] timeSlot = time.split("-");
            try {
                Date beginTime=null,endTime=null;
                String nowDateStr=DateUtils.convertDateToString(new Date());
                beginTime= DateUtils.convertStringToDate("yyyy-MM-dd HH:mm", nowDateStr + " " + timeSlot[0]);
                Integer currentHour=Integer.valueOf(DateUtils.getDateTime("HH", beginTime));
                endTime=DateUtils.convertStringToDate("yyyy-MM-dd HH:mm",nowDateStr+" "+timeSlot[1]);
                if(currentHour==21 && beginTime.after(endTime)){
                    endTime=DateUtils.addDay(endTime,1);
                }
                long between=(endTime.getTime()-beginTime.getTime())/1000;//除以1000是为了转换成秒
                long min=between/60;
                LOGGER.info(time + "min:" + min);
                minNum+=min;
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return minNum;
    }

    public static void isNowTime(){
        boolean flag=false;
        String time = "2018-08-01 23:20:45";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
        LocalDateTime localTime = LocalDateTime.parse(time, dtf);
        LocalDateTime startTime = LocalDate.now().atTime(0, 0, 0);
        LocalDateTime endTime = LocalDate.now().atTime(23, 59, 59);
        //如果小于今天的开始日期
        if (localTime.isBefore(startTime)) {
            LOGGER.info("时间是过去");
        }
        //如果大于今天的开始日期，小于今天的结束日期
        if (localTime.isAfter(startTime) && localTime.isBefore(endTime)) {
           flag=true;
        }
        //如果大于今天的结束日期
        if (localTime.isAfter(endTime)) {
            LOGGER.info("时间是未来");
        }
    }



    public static void main(String[] args) {
        BigDecimal minPrice=new BigDecimal(0.0001).setScale(4,BigDecimal.ROUND_HALF_UP);
        System.err.println("minPrice:"+minPrice);
    }

    public static void testState(){
        String aa="a|1|0";
        String b="1";
        String c=aa.substring(aa.length()-1);
        if("5".equals(c) || "0".equals(c)){
            String aaa=aa.substring(0,aa.length()-1);
            String cc=aaa+b+"|"+c;
            System.err.println("c......"+cc);
        }
    }
}
